package jarvis.fs;

import jarvis.fs.document.DocumentVector;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.store.Directory;

/**
 * 
 * Function: The Term Contribution measure to get feature terms
 * @author Jarvis.Guo
 *
 */
public class TC extends FeatureSelection {

	/**
	 * @param dir
	 */
	public TC(Directory dir) {
		super(dir);
		
	}

	/* (non-Javadoc)
	 * @see jarvis.fs.FeatureSelection#getTopNFeature()
	 */
	@Override
	public List<ComparableTerm> getFeatures() {
		List<ComparableTerm> result = new ArrayList<ComparableTerm>();
		//init document tfidf vectors
		DocumentVector[] vectors = DocumentVector.getVectors(indexReader);
		try{
			TermEnum te = indexReader.terms();
			int termNum = 0;
			
			while(te.next())
			{
				Term term = te.term();
				double value = 0.0;
				//sum of the product of term's tfidf in all different documents pairs
				for(int i=0;i<vectors.length;i++)
				{
					for(int j=i+1;j<vectors.length;j++)
					{
						value += vectors[i].getTermNumTFIDF(termNum) * vectors[j].getTermNumTFIDF(termNum);
						
					}
				}
				result.add(new DoubleComparableTerm(term,value));
				termNum++;
			}
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
		
		return result;
	}

}
